//获取应用实例
var api = require('../../../config/api.js');
var app = getApp()
Page({
  data: {
    userName: '',
    userPassword: '',
    openid: '',
  },


  onLoad: function () {
    var that = this
    //此处需要请求数据校验token是否失效，如果失效则需要重新登录
    if (wx.getStorageSync('token')) {
      console.info("token=" + wx.getStorageSync('token'));
      that.tokenValidate();
    } else {
      that.checkOpenid()
    }
  },

  onShow:function(){
    var that = this
    if (wx.getStorageSync('openId')) {
      that.setData({
        openid: wx.getStorageSync('openId')
      })
    }
  },

  // 获取输入账号
  phoneInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  // 获取输入密码
  passwordInput: function (e) {
    this.setData({
      userPassword: e.detail.value
    })
  },

  // 登录
  login: function () {
    if (this.data.userName.length == 0 || this.data.userPassword.length == 0) {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'loading',
        duration: 2000
      })
    } else {
      // 这里修改成跳转的页面
      //开始登录请求
      wx.showLoading({
        title: '登录中',
      })
      this.loginAjax();
    }
  },

  //验证openid
  checkOpenid: function () {
    var that = this
    if (wx.getStorageSync('openId')) {
      that.setData({
        openid: wx.getStorageSync('openId')
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  loginAjax: function () {
    var that = this
    wx.request({
      url: api.AuthLogin + '?username=' + this.data.userName + '&password=' + this.data.userPassword + '&wxType=SALER' + '&openId=' + this.data.openid,
      data: {},
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: '登录成功',
            icon: 'none',
            duration: 2000,
            success: function () {
              var id_token = res.data.obj.token;
              if (id_token) {
                console.log(id_token)
                wx.setStorageSync('token', id_token)
                wx.setStorageSync('userName', res.data.obj.user.username)
                wx.setStorageSync('trueName', res.data.obj.user.trueName)
                wx.setStorageSync('usertype', res.data.obj.user.roleIds)
                wx.setStorageSync('status', res.data.obj.user.status)
                wx.setStorageSync('deptId', res.data.obj.user.deptId)
                wx.setStorageSync('id', res.data.obj.user.id)
                console.log('usertype=' + wx.getStorageSync('usertype'))
              }
              wx.request({
                url: api.GetAuthInfo,
                data: {},
                method: 'GET',
                header: {
                  'Accept': 'application/json',
                  'ACCESS-TOKEN': wx.getStorageSync('token')
                },
                success: (res) => {
                  if (res.data.success) {
                    wx.setStorageSync('userId', res.data.obj.id)
                    console.log(wx.getStorageInfoSync('userId'))
                  }
                  wx.switchTab({
                    url: '/pages/manage/manage'
                  })
                }

              })

            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      },
      fail: function (res) {
        wx.showToast({
          title: '登录失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //校验token
  tokenValidate: function () {
    var that = this
    var token = wx.getStorageSync('token')
    wx.request({
      url: api.CheckToken,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.data.success) {
          console.log('当前token有效==' + token)
          wx.switchTab({
            url: '/pages/manage/manage'
          })
        } else {
          wx.showToast({
            title: '登录已失效，请重新登录！',
            icon: 'none',
            duration: 2000
          })
          wx.hideLoading();

        }

      }
    })

  },
  register: function(){
    wx.navigateTo({
       url: '/pages/salermg/saleradd',
      // url: '/pages/register/register',
    })
  },
  forgetPass: function () {
    wx.navigateTo({
      url: 'pages/register/register',
    })
  }
})